<template>
  <div class="dashboard-container" v-loading='loading'>
    <div class="app-container" >
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <!-- <tree-tools slot-scope="{ data }" :tree-node="data" @del-depts="getDepartments"/> -->
          <tree-tools
            slot-scope="obj"
            :tree-node="obj.data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <add-dept
        ref="addDept"
        :pid="node"
        @getDepartments="getDepartments"
        :showDialog.sync="showDialog"
      ></add-dept>
    </div>
  </div>
</template>

<script>
import treeTools from "./components/tree-tools.vue";
import addDept from "./components/add-dept.vue";
import { getDepartment } from "@/api/department.js";
import { tranListToTreeData } from "@/utils";
export default {
  components: { treeTools, addDept },
  data() {
    return {
      company: {}, // 就是头部的数据结构
      departs: [],
      showDialog: false, //显示新增窗口
      defaultProps: {
        label: "name", // 表示 从这个属性显示内容
      },
      node: "",
      loading:false,
    };
  },
  methods: {
    async getDepartments() {
      this.loading = true
      let res = await getDepartment();
      this.showDialog = false;
      this.company = { name: res.companyName, manager: "负责人", id: "" };
      this.departs = tranListToTreeData(res.depts, "");
      this.loading = false
    },
    // 新增部门
    addDepts(val) {
      this.showDialog = true;
      this.node = val;
    },
    // 编辑部门
    async editDepts(val) {
      // this.node = val;
      await this.$refs.addDept.getDepartDetail(val.id);
      // 打开之前就要获取数据，所以加了 await
      this.showDialog = true;
    },
  },
  async created() {
    this.getDepartments();
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>

